package com.telchina.workorder.controller;

import cn.hutool.core.lang.UUID;
import com.telchina.common.annotation.Log;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.core.domain.entity.SysUser;
import com.telchina.common.enums.BusinessType;
import com.telchina.common.enums.WorkOrderStatusEnum;
import com.telchina.common.utils.DateUtils;
import com.telchina.common.utils.SecurityUtils;
import com.telchina.common.utils.poi.ExcelUtil;
import com.telchina.workorder.domain.WoWorkOrder;
import com.telchina.workorder.domain.vo.WoWorkOrderReqVO;
import com.telchina.workorder.domain.vo.WoWorkOrderVO;
import com.telchina.workorder.service.IWoWorkOrderService;
import com.telchina.workorder.third.service.SystemThirdService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工单信息（受理单）Controller
 *
 * @author konghao
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/workorder/woWorkOrder")
@Log4j2
public class WoWorkOrderController extends BaseController {
    @Autowired
    private IWoWorkOrderService woWorkOrderService;
    @Autowired
    private SystemThirdService systemThirdService;


    /**
     * 分页查询工单信息（受理单）列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody WoWorkOrderReqVO woWorkOrder) {
        List<WoWorkOrderVO> list = woWorkOrderService.selectListAll(woWorkOrder);
        return getDataTable(list);
    }

    /**
     * 分页查询未处结工单信息（受理单）列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @PostMapping("/listNoEnd")
    public AjaxResult listNoEnd(@RequestBody WoWorkOrderReqVO woWorkOrder) {
        List<WoWorkOrder> list = woWorkOrderService.selectListAllNoEnd(woWorkOrder);
        return getDataTable(list);
    }
    /**
     * 分页查询未处结工单信息（受理单）列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @GetMapping("/listMerge")
    public AjaxResult listMerge(WoWorkOrderReqVO woWorkOrder) {
        List<WoWorkOrder> list = woWorkOrderService.selectListMerge(woWorkOrder);
        return getDataTable(list);
    }



    /**
     * 查询工单信息（受理单）列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @GetMapping("/list/all")
    public AjaxResult listAll(WoWorkOrderReqVO woWorkOrder) {
        List<WoWorkOrderVO> list = woWorkOrderService.selectListAll(woWorkOrder);
        return AjaxResult.success(list);
    }

    /**
     * 导出工单信息（受理单）列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:export')")
    @Log(title = "工单信息（受理单）", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WoWorkOrderReqVO woWorkOrder) {
        List<WoWorkOrderVO> list = woWorkOrderService.selectListAll(woWorkOrder);
        ExcelUtil<WoWorkOrderVO> util = new ExcelUtil<WoWorkOrderVO>(WoWorkOrderVO.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 获取工单信息（受理单）详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(woWorkOrderService.selectById(id));
    }

    /**
     * 获取工单信息（受理单）详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:query')")
    @GetMapping(value = "/workOrderNumber/{workOrderNumber}")
    public AjaxResult getWorkOrderByWorkOrderNumber(@PathVariable("workOrderNumber") String workOrderNumber) {
        return AjaxResult.success(woWorkOrderService.getWorkOrderByWorkOrderNumber(workOrderNumber));
    }

    /**
     * 新增工单信息（受理单）
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:add')")
    @Log(title = "工单信息（受理单）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WoWorkOrder woWorkOrder) {
        if (woWorkOrder.getWorkOrderStatus().equals(WorkOrderStatusEnum.FINISH.getCode())) {
            woWorkOrder.setCompletionTime(new Date());
        }
        woWorkOrder.setRegistrant(SecurityUtils.getLoginUser().getUserId());
        woWorkOrder.setRegistrantDeptId(systemThirdService.getLoginUseDeptId());
        return woWorkOrderService.insert(woWorkOrder);
    }

    /**
     * 派单处理
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:add')")
    @Log(title = "派单处理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/dispatchWorkOrder")
    public AjaxResult dispatchWorkOrder(@RequestBody Map<String, Object> maps) {
        log.info("派单业务流程开始处理！！！！" + maps);
        return woWorkOrderService.dispatchWorkOrder(maps);
    }

    /**
     * 修改工单信息（受理单）
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:edit')")
    @Log(title = "工单信息（受理单）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WoWorkOrder woWorkOrder) {
        return woWorkOrderService.update(woWorkOrder);
    }

    /**
     * 删除工单信息（受理单）
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:remove')")
    @Log(title = "工单信息（受理单）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(woWorkOrderService.deleteByIds(ids));
    }


    /**
     * 初始化是设置工单单号  受理时间  受理单号等信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:add')")
    @GetMapping("/getInitWorkOrder")
    public AjaxResult getInitWorkOrder() {
        Map<String, Object> result = new HashMap<>();
        // 登录用户账号信息
        SysUser sysUser = systemThirdService.getSysUserInfo(SecurityUtils.getLoginUser().getUserId());
        result.put("id", UUID.fastUUID().toString(Boolean.TRUE));
        result.put("acceptNumber", sysUser.getLoginName());
        result.put("workOrderNumber", DateUtils.parseDateToStr("yyMMdd", new Date()));
        result.put("acceptTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
        return AjaxResult.success(result);
    }

    /**
     * 工作流程相关接口开始
     */
    /**
     * 分页查询工单信息我的代办列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @GetMapping("todoList")
    public AjaxResult todoList(WoWorkOrderReqVO woWorkOrder) {
        List<WoWorkOrderVO> list = woWorkOrderService.selectTodoList(woWorkOrder);
        return getDataTable(list);
    }

    /**
     * 获得指定流程实例的任务列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @GetMapping("list-by-process-instance-id")
    public AjaxResult getTaskListByProcessInstanceId(String processInstanceId) {
        return AjaxResult.success(woWorkOrderService.getTaskListByProcessInstanceId(processInstanceId)) ;
    }

    /**
     * 工单业务流转
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:add')")
    @Log(title = "工单转派", businessType = BusinessType.INSERT)
    @PostMapping(value = "/sendLastWorkOrder")
    public AjaxResult sendLastWorkOrder(@RequestBody Map<String, Object> maps) {
        log.info("工单业务开始处理！！！！" + maps);
        return woWorkOrderService.sendLastWorkOrder(maps);
    }

    /**
     * 并单和解除并单操作
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @PostMapping("changeMergeWorkOrderHandle")
    public AjaxResult changeMergeWorkOrderHandle(@RequestBody Map<String, Object> maps) {
        return woWorkOrderService.changeMergeWorkOrderHandle(maps) ;
    }
}
